package com.forward.controller;

import com.alibaba.fastjson.JSON;
import com.forward.entity.LoginSysPar;
import com.forward.entity.SendCodePar;
import com.forward.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: shafei
 * @Date: 2025/02/06 15:24
 * @description： 登录控制层
 **/
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Value("${doll.forward.server.url}")
    private String url;

    /**
     * 发送验证码接口
     */
    @PostMapping("/sendCode")
    public String sendCode(@RequestBody SendCodePar sendCodeBody) {

        log.info("sendCodeBody:{}",sendCodeBody);
        String sendCodeResponse = null;
        try {
            //将sendCodeBody对象转json字符串
            String sendCodeBodyJson = JSON.toJSONString(sendCodeBody);
            sendCodeResponse = HttpUtil.sendPostRequest(url+"/sys/smsCode", sendCodeBodyJson, "1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("GET Response: " + sendCodeResponse);
        return sendCodeResponse;
    }


    /**
     * 手机号登录接口
     */
    @PostMapping("/login")
    public String login(@RequestBody LoginSysPar loginBody) {
        String loginResponse = null;
        try {
            String sendCodeBodyJson = JSON.toJSONString(loginBody);
            loginResponse = HttpUtil.sendPostRequest(url+"/sys/wxphoneLogin", sendCodeBodyJson, "1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("GET Response: " + loginResponse);
        return loginResponse;
    }


    /**
     * 手机号登录接口
     */
    @PostMapping("/wxUserExit")
    public String wxUserExit(@RequestBody LoginSysPar loginBody) {
        String loginResponse = null;
        try {
            String sendCodeBodyJson = JSON.toJSONString(loginBody);
            loginResponse = HttpUtil.sendPostRequest(url+"/sys/wxlogout", sendCodeBodyJson, "1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("GET Response: " + loginResponse);
        return loginResponse;
    }

    /**
     * 获取用户信息接口
     */
    @PostMapping("/wxUserInfo")
    public String wxUserInfo(@RequestBody LoginSysPar loginBody) {
        String loginResponse = null;
        try {
            String sendCodeBodyJson = JSON.toJSONString(loginBody);
            loginResponse = HttpUtil.sendPostRequest(url+"/sys/user/wxUserInfo", sendCodeBodyJson, loginBody.getToken());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("GET Response: " + loginResponse);
        return loginResponse;
    }


}
